package me.sealer.ssm.transport;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 功能描述： $ HttpTransport
 *
 * @author sealer
 * @email 1178884049@qq.com
 * @date 2019年12月03日 16时41分34秒
 */
public class HttpTransport {
    public static String post(String url, NameValuePair[] data) throws IOException {
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(url);
        //在头文件中设置转码
        post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf8");

        post.setRequestBody(data);
        client.executeMethod(post);
        String result = new String(post.getResponseBodyAsString().getBytes(StandardCharsets.UTF_8));

        post.releaseConnection();

        return result;
    }
}
